package com.dev.environment;

import com.dev.environment.bean.EnvironmentBean;
import com.dev.environment.bean.ModuleBean;
import dev.environment.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.utils.TextTool;
import ohos.app.Context;

import java.util.List;

/**
 * News list adapter
 */
public class ListAdapter extends BaseItemProvider {
    private List<AdapterItem> infoList;
    private Context context;

    public ListAdapter(List<AdapterItem> infoList, Context context) {
        this.infoList = infoList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return infoList == null ? 0 : infoList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.infoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer componentContainer) {
        final AdapterItem item = (AdapterItem) getItem(position);
        switch (item.itemType) {
            case AdapterItem.MODULE_TYPE: // Module Type
                convertView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dev_environment_item_module, null, true);
                Text vid_deim_name_tv = (Text) convertView.findComponentById(ResourceTable.Id_vid_deim_name_tv);

                ModuleBean moduleBean = item.moduleBean;
                String moduleName = moduleBean.getName();
                String moduleAlias = moduleBean.getAlias();
                vid_deim_name_tv.setText(TextTool.isNullOrEmpty(moduleAlias) ? moduleName : moduleAlias);
                break;
            case AdapterItem.ENVIRONMENT_TYPE: // Environment Type
                convertView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dev_environment_item_environment, null, true);
                Text vid_deie_name_tv = (Text) convertView.findComponentById(ResourceTable.Id_vid_deie_name_tv);
                Text vid_deie_value_tv = (Text) convertView.findComponentById(ResourceTable.Id_vid_deie_value_tv);
                Image vid_deie_mark_igview = (Image) convertView.findComponentById(ResourceTable.Id_vid_deie_mark_igview);
                final EnvironmentBean environmentBean = item.environmentBean;
                String environmentName = environmentBean.getName();
                String environmentAlias = environmentBean.getAlias();
                vid_deie_name_tv.setText(TextTool.isNullOrEmpty(environmentAlias) ? environmentName : environmentAlias);
                vid_deie_value_tv.setText(environmentBean.getValue());
                vid_deie_mark_igview.setVisibility(item.isSelect() ? Component.VISIBLE : Component.INVISIBLE);

                convertView.setClickedListener(component -> {
                    if (Utils.setModuleEnvironment(component.getContext(), environmentBean)) {
//                        AdapterItem.refreshHashCode(component.getContext());
                        AdapterItem.changeHashCode(environmentBean);
                        notifyDataChanged();
                    }
                });
                break;
        }
        return convertView;
    }
}
